package com.education.webservlet;

import com.education.db.HibernateSessionFactory;
import com.education.db.dao.InfoUserDAO;
import com.education.db.domain.InfoUser;
import com.education.service.bean.UserBean;
import com.education.util.Constant;
import com.education.util.MD5Util;
import org.hibernate.Transaction;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by Fan307 on 2016/11/3.
 */
@WebServlet(name = "AdminLoginServlet")
public class AdminLoginServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private Transaction tran;

    public AdminLoginServlet() {

        super();
    }

    public void destroy() {
        super.destroy();
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        this.doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding(Constant.CHAR_ENCODING);

        tran = HibernateSessionFactory.getSession().getTransaction();

        String phone = request.getParameter("phone");
        String password = request.getParameter("password");
        password = MD5Util.MD5(password);
        try {
            tran.begin();
            InfoUserDAO infoUserDAO = new InfoUserDAO();
            InfoUser infoUser = (InfoUser) infoUserDAO.findByUsPhone(phone);
            short type = (short)infoUser.getUsType();
            if (infoUser == null) {
                String info = "用户不存在";
                request.setAttribute("info", info);
                request.getRequestDispatcher("/Pages/message.jsp").forward(request,
                        response);
            } else if (!infoUser.getUsPassword().equals(password)) {
                String info="密码错误";
                request.setAttribute("info", info);
                request.getRequestDispatcher("/Pages/message.jsp").forward(request,
                        response);
            } else if (type != 3)  {
                String info = "用户权限不够";
                request.setAttribute("info", info);
                request.getRequestDispatcher("/Pages/message.jsp").forward(request,
                        response);
            }
            else {
//                userLoginLog(infoUser);
                UserBean userBean = new UserBean();
                userBean.setPhone(infoUser.getUsPhone());
                userBean.setUserID(infoUser.getUsId());
                request.getSession().setAttribute("userBean", userBean);
                request.getRequestDispatcher("/admin/addFiles.jsp").forward(request, response);
                tran.commit();
            }
        } catch (Exception e) {
            System.out.println(e.toString());
            tran.rollback();
        }
    }

    /**
     * Initialization of the servlet. <br>
     *
     * @throws ServletException
     *             if an error occurs
     */
    public void init() throws ServletException {

    }

}
